package com.ayuku.retail.ui.kasir;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ayuku.commons.AYConst;
import com.ayuku.commons.AyBigDecimal;
import com.ayuku.commons.AyInteger;
import com.ayuku.commons.AyukuDB;
import com.ayuku.commons.Util;
import com.ayuku.retail.business.stock.StockImpl;
import com.ayuku.retail.persistence.model.Stock;
import com.ayuku.retail.persistence.model.TrxTempSale;
import com.ayuku.retail.ui.stock.StockLookup;


public class SalesCash extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private Grid tableData;
	private GridColumn tableColumn1;
	private Label label11;
	public AyBigDecimal txtPPN;
	private Label label10;
	public AyBigDecimal txtPPn;
	private Label label1;
	public Label txtDisplayTotal;
	private AyBigDecimal txtQty;
	public AyBigDecimal txtDiscountPercent;
	public AyBigDecimal txtGrandTotal;
	public AyBigDecimal txtDiscAmount;
	private Label label9;
	private AyBigDecimal txtSubTotal;
	private Label label7;
	private Composite composite4;
	private Text txtKasir;
	private Text txtDate;
	private Label label8;
	private Label label6;
	private AyInteger txtTrxNo;
	private Label label5;
	private Composite composite3;
	private Composite composite2;
	private Composite composite1;
	private Label label4;
	private Text txtCode;
	private Label label3;
	private Label label2;
	private GridColumn tableColumn7;
	private GridColumn tableColumn6;
	private GridColumn tableColumn5;
	private GridColumn tableColumn4;
	private GridColumn tableColumn3;
	private GridColumn tableColumn2;
	private String funcId;

	public String getFuncId() {
		return funcId;
	}

	public void setFuncId(String funcId) {
		this.funcId = funcId;
	}

	/**
	 * Auto-generated main method to display this org.eclipse.swt.widgets.Dialog
	 * inside a new Shell.
	 */
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			SalesCash inst = new SalesCash(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public SalesCash(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {

			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE
					| SWT.APPLICATION_MODAL);
			dialogShell.setSize(914, 558);

			FormLayout thisLayout = new FormLayout();
			dialogShell.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(176, 196, 222));

			dialogShell.setLayout(thisLayout);
			dialogShell.setLayout(thisLayout);

			dialogShell.setLayout(thisLayout);

			composite4 = new Composite(dialogShell, SWT.NONE);
			composite4.setLayout(new GridLayout(4, false));
			FormData composite4LData = new FormData();
			composite4LData.width = 381;
			composite4LData.height = 115;
			composite4LData.bottom = new FormAttachment(1000, 1000, -31);
			composite4LData.right = new FormAttachment(1000, 1000, 0);
			composite4.setLayoutData(composite4LData);
			composite4.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(176, 196, 222));
			composite4.setEnabled(false);

			label7 = new Label(composite4, SWT.NONE);
			label7.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
					false, 1, 1));
			label7.setText("SUB TOTAL:");
			label7.setAlignment(SWT.RIGHT);
			label7.setFont(SWTResourceManager.getFont("Tahoma", 11, 0, false,
					false));
			label7.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(176, 196, 222));

			txtSubTotal = new AyBigDecimal(composite4, SWT.BORDER);
			txtSubTotal.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
					false, 3, 1));
			txtSubTotal.setFont(SWTResourceManager.getFont("Tahoma", 11, 0,
					false, false));
			txtSubTotal.setEnabled(false);

			label3 = new Label(composite4, SWT.NONE);
			label3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
					false, 1, 1));
			label3.setText("DISCOUNT:");
			label3.setFont(SWTResourceManager.getFont("Tahoma", 11, 0, false,
					false));
			label3.setAlignment(SWT.RIGHT);
			label3.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(176, 196, 222));

			txtDiscountPercent = new AyBigDecimal(composite4, SWT.BORDER);
			GridData gd_txtDiscountPercent = new GridData(SWT.LEFT, SWT.CENTER,
					false, false, 1, 1);
			gd_txtDiscountPercent.widthHint = 46;
			txtDiscountPercent.setLayoutData(gd_txtDiscountPercent);
			txtDiscountPercent.setFont(SWTResourceManager.getFont("Tahoma", 11,
					0, false, false));
			txtDiscountPercent.setEnabled(false);

			label10 = new Label(composite4, SWT.NONE);
			label10.setText("%  ");
			label10.setAlignment(SWT.RIGHT);
			label10.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(176, 196, 222));
			label10.setFont(SWTResourceManager.getFont("Tahoma", 11, 0, false,
					false));

			txtDiscAmount = new AyBigDecimal(composite4, SWT.BORDER);
			txtDiscAmount.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
					true, false, 1, 1));
			txtDiscAmount.setFont(SWTResourceManager.getFont("Tahoma", 11, 0,
					false, false));
			txtDiscAmount.setEnabled(false);

			label1 = new Label(composite4, SWT.NONE);
			label1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
					false, 1, 1));
			label1.setText("PPN:");
			label1.setAlignment(SWT.RIGHT);
			label1.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(176, 196, 222));
			label1.setFont(SWTResourceManager.getFont("Tahoma", 11, 0, false,
					false));

			txtPPn = new AyBigDecimal(composite4, SWT.BORDER);
			txtPPn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
					false, 1, 1));
			txtPPn.setFont(SWTResourceManager.getFont("Tahoma", 11, 0, false,
					false));
			txtPPn.setEnabled(false);

			txtPPn.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent evt) {
					text1KeyPressed(evt);
				}
			});

			label11 = new Label(composite4, SWT.NONE);
			label11.setText("%  ");
			label11.setAlignment(SWT.RIGHT);
			label11.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(176, 196, 222));
			label11.setFont(SWTResourceManager.getFont("Tahoma", 11, 0, false,
					false));

			txtPPN = new AyBigDecimal(composite4, SWT.BORDER);
			txtPPN.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
					false, 1, 1));
			txtPPN.setEnabled(false);
			txtPPN.setFont(SWTResourceManager.getFont("Tahoma", 11, 0, false,
					false));

			label9 = new Label(composite4, SWT.NONE);
			label9.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
					false, 1, 1));
			label9.setText("GRAND TOTAL:");
			label9.setAlignment(SWT.RIGHT);
			label9.setFont(SWTResourceManager.getFont("Tahoma", 11, 0, false,
					false));
			label9.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(176, 196, 222));

			txtGrandTotal = new AyBigDecimal(composite4, SWT.BORDER);
			txtGrandTotal.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
					false, false, 3, 1));
			txtGrandTotal.setFont(SWTResourceManager.getFont("Tahoma", 11, 0,
					false, false));
			txtGrandTotal.setEnabled(false);

			tableData = new Grid(dialogShell, SWT.BORDER | SWT.FULL_SELECTION
					| SWT.V_SCROLL);
			tableData.setHeaderVisible(true);
			tableData.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Tahoma", 12, SWT.NORMAL));
			tableData.setItemHeight(18);

			tableColumn1 = new GridColumn(tableData, SWT.NONE);
			tableColumn1.setText("KODE");
			tableColumn1.setResizeable(true);
			tableColumn1.setWidth(150);

			tableColumn2 = new GridColumn(tableData, SWT.NONE);
			tableColumn2.setText("KETERANGAN");
			tableColumn2.setResizeable(true);
			tableColumn2.setWidth(250);

			tableColumn3 = new GridColumn(tableData, SWT.NONE);
			tableColumn3.setText("HARGA");
			tableColumn3.setResizeable(true);
			tableColumn3.setWidth(150);
			tableColumn3.setAlignment(SWT.RIGHT);

			tableColumn4 = new GridColumn(tableData, SWT.NONE);
			tableColumn4.setText("QTY");
			tableColumn4.setResizeable(true);
			tableColumn4.setWidth(80);
			tableColumn4.setAlignment(SWT.RIGHT);

			tableColumn5 = new GridColumn(tableData, SWT.NONE);
			tableColumn5.setText("DISC.(%)");
			tableColumn5.setResizeable(true);
			tableColumn5.setWidth(100);
			tableColumn5.setAlignment(SWT.RIGHT);

			tableColumn6 = new GridColumn(tableData, SWT.NONE);
			tableColumn6.setText("TOTAL");
			tableColumn6.setResizeable(true);
			tableColumn6.setWidth(120);
			tableColumn6.setAlignment(SWT.RIGHT);

			tableColumn7 = new GridColumn(tableData, SWT.NONE);
			tableColumn7.setText("idx");
			tableColumn7.setResizeable(false);
			tableColumn7.setWidth(0);

			composite1 = new Composite(dialogShell, SWT.NONE);
			composite1.setLayout(new GridLayout(2, false));
			FormData composite1LData = new FormData();
			composite1LData.width = 344;
			composite1LData.height = 60;
			composite1LData.left = new FormAttachment(0, 1000, 4);
			composite1LData.bottom = new FormAttachment(1000, 1000, -72);
			composite1.setLayoutData(composite1LData);
			composite1
					.setBackground(SWTResourceManager.getColor(128, 128, 255));

			composite2 = new Composite(dialogShell, SWT.NONE);
			FillLayout composite2Layout = new FillLayout(
					org.eclipse.swt.SWT.HORIZONTAL);
			composite2.setLayout(composite2Layout);
			FormData composite2LData = new FormData();
			composite2LData.width = 1019;
			composite2LData.height = 34;
			composite2LData.top = new FormAttachment(0, 1000, 74);
			composite2LData.left = new FormAttachment(0, 1000, 0);
			composite2LData.right = new FormAttachment(1000, 1000, 0);
			composite2.setLayoutData(composite2LData);
			composite2
					.setBackground(SWTResourceManager.getColor(153, 204, 255));

			txtDisplayTotal = new Label(dialogShell, SWT.NONE);
			FormData txtDisplayTotalLData = new FormData();
			txtDisplayTotalLData.left = new FormAttachment(0, 1000, -3);
			txtDisplayTotalLData.top = new FormAttachment(0, 1000, 0);
			txtDisplayTotalLData.width = 1022;
			txtDisplayTotalLData.height = 68;
			txtDisplayTotalLData.right = new FormAttachment(1000, 1000, 0);

			txtDisplayTotal.setLayoutData(txtDisplayTotalLData);
			txtDisplayTotal.setAlignment(SWT.RIGHT);
			txtDisplayTotal.setBackground(SWTResourceManager.getColor(0, 0, 0));
			txtDisplayTotal.setFont(SWTResourceManager.getFont("Arial", 48, 1,
					false, false));
			txtDisplayTotal.setForeground(SWTResourceManager
					.getColor(0, 255, 0));

			// txtDisplayTotal.setAlignment(SWT.RIGHT);

			composite3 = new Composite(composite2, SWT.NONE);
			composite3.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(176, 196, 222));
			composite3.setLayout(new GridLayout(6, false));

			label5 = new Label(composite3, SWT.NONE);
			GridData gd_label5 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
			gd_label5.widthHint = 149;
			label5.setLayoutData(gd_label5);
			label5.setText("No. Transaksi:");
			label5.setFont(SWTResourceManager.getFont("Courier", 12, 0, false,
					false));
			label5.setAlignment(SWT.RIGHT);
			label5.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(176, 196, 222));

			txtTrxNo = new AyInteger(composite3, SWT.BORDER);
			GridData gd_txtTrxNo = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
			gd_txtTrxNo.widthHint = 65;
			txtTrxNo.setLayoutData(gd_txtTrxNo);
			txtTrxNo.setFont(SWTResourceManager.getFont("Courier", 12, 0,
					false, false));
			txtTrxNo.getOrgComponent().getControl().setEditable(false);
			txtTrxNo.setEnabled(false);

			label6 = new Label(composite3, SWT.NONE);
			GridData gd_label6 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
			gd_label6.widthHint = 95;
			label6.setLayoutData(gd_label6);
			label6.setText("Tanggal:");
			label6.setFont(SWTResourceManager.getFont("Courier", 12, 0, false,
					false));
			label6.setAlignment(SWT.RIGHT);
			label6.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(176, 196, 222));

			txtDate = new Text(composite3, SWT.BORDER);
			GridData gd_txtDate = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
			gd_txtDate.widthHint = 180;
			txtDate.setLayoutData(gd_txtDate);
			txtDate.setFont(SWTResourceManager.getFont("Courier", 12, 0, false,
					false));
			txtDate.setEnabled(false);

			label8 = new Label(composite3, SWT.NONE);
			GridData gd_label8 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
			gd_label8.widthHint = 63;
			label8.setLayoutData(gd_label8);
			label8.setText("User:");
			label8.setFont(SWTResourceManager.getFont("Courier", 12, 0, false,
					false));
			label8.setAlignment(SWT.RIGHT);
			label8.setBackground(org.eclipse.wb.swt.SWTResourceManager
					.getColor(176, 196, 222));

			txtKasir = new Text(composite3, SWT.BORDER);
			GridData gd_txtKasir = new GridData(SWT.LEFT, SWT.CENTER, false,
					false, 1, 1);
			gd_txtKasir.widthHint = 196;
			txtKasir.setLayoutData(gd_txtKasir);
			txtKasir.setFont(SWTResourceManager.getFont("Courier", 12, 0,
					false, false));
			txtKasir.setEnabled(false);

			label4 = new Label(composite1, SWT.NONE);
			label4.setText("Qty:");
			label4.setAlignment(SWT.RIGHT);
			label4.setFont(SWTResourceManager.getFont("Courier", 12, 1, false,
					false));
			label4.setBackground(SWTResourceManager.getColor(128, 128, 255));

			txtQty = new AyBigDecimal(composite1, SWT.BORDER);
			GridData gd_txtQty = new GridData(SWT.LEFT, SWT.CENTER, true,
					false, 1, 1);
			gd_txtQty.widthHint = 57;
			txtQty.setLayoutData(gd_txtQty);
			txtQty.setFont(SWTResourceManager.getFont("Courier", 12, 1, false,
					false));
			txtQty.setStringFormatter("###,##0.####");

			txtQty.getOrgComponent().getControl()
					.addFocusListener(new FocusAdapter() {
						public void focusLost(FocusEvent evt) {
							txtQtyFocusLost(evt);
						}

						public void focusGained(FocusEvent evt) {
							txtQtyFocusGained(evt);
						}
					});
			txtQty.getOrgComponent().getControl()
					.addKeyListener(new KeyAdapter() {
						public void keyPressed(KeyEvent evt) {
							txtQtyKeyPressed(evt);
						}
					});
			txtQty.setValue(BigDecimal.valueOf(1));

			label2 = new Label(composite1, SWT.NONE);
			label2.setText("Barcode:");
			label2.setFont(SWTResourceManager.getFont("Courier", 12, 1, false,
					false));
			label2.setAlignment(SWT.RIGHT);
			label2.setBackground(SWTResourceManager.getColor(128, 128, 255));

			txtCode = new Text(composite1, SWT.BORDER);
			txtCode.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
					1, 1));
			txtCode.setFont(SWTResourceManager.getFont("Courier", 12, 1, false,
					false));
			txtCode.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent evt) {
					txtCodeKeyPressed(evt);
				}
			});

			FormData table1LData = new FormData();
			table1LData.width = 1017;
			table1LData.height = 130;
			table1LData.top = new FormAttachment(0, 1000, 110);
			table1LData.bottom = new FormAttachment(1000, 1000, -148);
			table1LData.right = new FormAttachment(1000, 1000, 6);
			table1LData.left = new FormAttachment(0, 1000, 0);
			tableData.setItemCount(0);

			tableData.setLayoutData(table1LData);
			tableData.setAutoWidth(false);
			tableData.addMouseListener(new MouseAdapter() {
				public void mouseDoubleClick(MouseEvent evt) {
					tableDataMouseDoubleClick(evt);
				}
			});
			tableData.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent evt) {
					tableDataKeyPressed(evt);
				}
			});
			tableData.addControlListener(new ControlAdapter() {
				public void controlResized(ControlEvent evt) {
					tableDataControlResized(evt);
				}
			});
			// dialogShell.pack();

			int rect = tableData.getBounds().width;
			System.out.println("RESCT1: " + rect);

			// dialogShell.pack();
			dialogShell.layout();

			Util.setFull(dialogShell);
			txtCode.setFocus();

			setTotalDisplay(BigDecimal.valueOf(0));
			dialogShell.setMinimumSize(790, 590);
			// dialogShell.pack();
			// dialogShell.setMaximized(true);
			dialogShell.open();

			startTrx();

			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void tableDataControlResized(ControlEvent evt) {
		int rect = tableData.getBounds().width;
		int boun = tableData.getBounds().width;
		System.out.println("rect: " + rect);
		System.out.println("boun: " + boun);
		tableData.getColumnCount();

		int total = tableColumn1.getWidth() + tableColumn3.getWidth()
				+ tableColumn4.getWidth() + tableColumn5.getWidth()
				+ tableColumn6.getWidth();
		System.out.println("total2" + total);

		tableColumn2.setWidth(boun - total - 25);

	}

	private void startTrx() {
		Integer nextTrxNo=null;
		trxDateCal = Calendar.getInstance();
		Calendar cal = Calendar.getInstance();
		cal.setTime(trxDateCal.getTime());
		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		
		try {
			String sql = "select max(trx_code) from sales where DAY_OF_MONTH(trx_date)=" +trxDateCal.get(Calendar.DAY_OF_MONTH)+ " AND MONTH(trx_date)=" + (trxDateCal.get(Calendar.MONTH)+1) + " AND YEAR(trx_date)=" + trxDateCal.get(Calendar.YEAR);
			System.out.println("SQL: " + sql);
			nextTrxNo = AyukuDB.getPersist().read(Integer.class, sql);
			
			if (nextTrxNo==null){
				nextTrxNo = 1;
			}else{
				nextTrxNo += 1;
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		txtQty.setValue(BigDecimal.valueOf(1));
		
		//String f = AYConst.FOR_TRX_NO_FORMAT.format(trxDateCal.getTime());
		txtTrxNo.setValue(nextTrxNo);
		txtDate.setText(AYConst.TANGGAL_FORM_TRX.format(trxDateCal.getTime()));
		txtDate.setEditable(false);

		if (Util.getCurrentUser() != null) {
			txtKasir.setText(Util.getCurrentUser().getUname());
		} else {
			txtKasir.setText("--");
		}
		txtKasir.setEditable(false);
		
		
		try {
			AyukuDB.getPersist().executeUpdate(
					"delete from trx_temp_sale");
			doReloadList();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	

	}

	public Calendar getTrxDateCal() {
		return trxDateCal;
	}

	public void setTrxDateCal(Calendar trxDateCal) {
		this.trxDateCal = trxDateCal;
	}

	

	public AyInteger getTxtTrxNo() {
		return txtTrxNo;
	}

	public void setTxtTrxNo(AyInteger txtTrxNo) {
		this.txtTrxNo = txtTrxNo;
	}

	private void populateGridTable(String key) {
		System.out.println("barcode: " + key);
		Stock stk;
		try {
			stk = StockImpl.getByBarcode(key);
			if (stk != null) {
				System.out.println("*** Stock dicari ditemukan");

				TrxTempSale sale = AyukuDB.getPersist().readByPrimaryKey(
						TrxTempSale.class, key.trim());

				if (sale != null) {

					double totqty = sale.getQty().doubleValue()
							+ txtQty.getValue().doubleValue();

					sale.setQty(BigDecimal.valueOf(totqty));
					AyukuDB.getPersist().update(sale);

				} else {

					TrxTempSale n = new TrxTempSale();
					n.setBarcode(stk.getBarcode());
					n.setKeterangan(stk.getName());
					double harga = stk.getSellingPrice().doubleValue();
					n.setPrice(BigDecimal.valueOf(harga));
					double qty = txtQty.getValue().doubleValue();
					n.setQty(BigDecimal.valueOf(qty));

					Integer rowId = AyukuDB.getPersist().read(Integer.class,
							"select max(idx) from trx_temp_sale");
					if (rowId == null) {
						rowId = 0;
					}

					n.setIdx(rowId + 1);
					double discount = stk.getDiscount().doubleValue();
					n.setDisc(BigDecimal.valueOf(discount));
					n.setIpAddress(Util.getIPAddress());
					n.setTotal(BigDecimal.valueOf(0));
					AyukuDB.getPersist().insert(n);
				}
			} else {
				System.out.println("*** Stock dicari tidak ditemukan");
			}

			// recalculate
			doRecalculateRow(key);
			// end of

			doReloadList();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void doRecalculateRow(String key) throws SQLException {
		TrxTempSale sold = AyukuDB.getPersist().readByPrimaryKey(
				TrxTempSale.class, key.trim());

		if (sold != null) {
			double totalHarga = sold.getQty().doubleValue()
					* sold.getPrice().doubleValue();
			double totalDiskon = (sold.getDisc().doubleValue() / 100)
					* totalHarga;
			double totalNetto = totalHarga - totalDiskon;
			sold.setTotal(BigDecimal.valueOf(totalNetto));
			AyukuDB.getPersist().update(sold);

		}
	}

	private void doReloadList() throws SQLException {
		List<TrxTempSale> lst = AyukuDB.getPersist().readList(
				TrxTempSale.class, "SELECT * FROM TRX_TEMP_SALE order by idx");
		tableData.removeAll();
		tableData.clearAll(true);
		tableData.setItemCount(lst.size());
		Double tot = 0.0;
		for (int i = 0; i < lst.size(); i++) {
			TrxTempSale sl = lst.get(i);
			tot += sl.getTotal().doubleValue();
			GridItem itm = tableData.getItem(i);
			itm.setText(0, sl.getBarcode());
			itm.setText(1, sl.getKeterangan());
			itm.setText(2, Util.formatToNumber(sl.getPrice()));
			itm.setText(3, Util.formatToNumber(sl.getQty()));
			itm.setText(4, Util.formatToNumber(sl.getDisc()));
			itm.setText(5, Util.formatToNumber(sl.getTotal()));
			itm.setText(6, sl.getIdx() + "");

		}
		txtSubTotal.setValue(BigDecimal.valueOf(tot));
		txtGrandTotal.setValue(BigDecimal.valueOf(tot));
		setTotalDisplay(BigDecimal.valueOf(tot));
		txtCode.setFocus();
	}

	private void setTotalDisplay(BigDecimal val) {
		txtDisplayTotal.setText("TOTAL: " + Util.formatToNumber(val));
	}

	private void showLookupStock() {

		StockLookup lk = new StockLookup(getParent(), SWT.NONE) {
			public void setForm(TableItem itm) {
				String code = Util.nullToString(itm.getText(0));
				txtCode.setText(code);
				populateGridTable(code);
			}
		};
		lk.open();
		txtCode.setFocus();
	}

	private void doPrepareInput() {
		txtCode.setText("");
		txtQty.setValue(BigDecimal.valueOf(1));
	}

	private void txtCodeKeyPressed(KeyEvent event) {
		switch (event.keyCode) {
		case SWT.CR:
			if (txtCode.getText().trim() == "") {
				showLookupStock();
			} else {
				populateGridTable(txtCode.getText());
			}
			doPrepareInput();
			txtCode.setFocus();
			break;
		case SWT.F8:
			/*
			 * StockLookup lk = new StockLookup(getParent(), SWT.NONE) { public
			 * void setForm(TableItem itm) { // String kode =
			 * Util.nullToString(itm.getText(0)); String ket =
			 * Util.nullToString(itm.getText(1)); // txtNewPosisi.setText(kode);
			 * } }; lk.open();
			 */
			break;
		case SWT.F7:
			showLookupStock();
			doPrepareInput();
			break;
		case SWT.F2:
			tableData.setFocus();
			int cnt = tableData.getItemCount();
			if (cnt > 0) {
				tableData.setSelection(0);
				// Rectangle itm = tableData.getItem(cnt-1).getBounds(0);
				// tableData.setCellSelection(new Point(0, 0));
				// tableData.setFocusItem(cnt-1);
				// Point s = tableData.getCellSelection()[0];
			} else {
				txtCode.setFocus();
			}
			break;
		case SWT.ARROW_UP:
			txtQty.setFocus();
			break;
		case SWT.ARROW_RIGHT:
			txtCode.setFocus();
			break;
		case SWT.ARROW_LEFT:
			txtQty.setFocus();
			break;

		case SWT.END:
			double total = txtSubTotal.getValue().doubleValue();
			byr = new FormBayar(dialogShell, SWT.NONE);
			byr.setSubTotal(new Double(total));
		
			byr.setFrmSalescash(this);
			byr.open();
			startTrx();
			break;

		}

	}

	private void txtQtyKeyPressed(KeyEvent evt) {
		if (evt.keyCode == SWT.CR) {
			txtCode.setFocus();
		} else if (evt.keyCode == SWT.ARROW_DOWN) {
			txtCode.setFocus();
		}else if (evt.keyCode == SWT.END){
			txtCode.setFocus();
			double total = txtSubTotal.getValue().doubleValue();
			byr = new FormBayar(dialogShell, SWT.NONE);
			byr.setSubTotal(new Double(total));
			byr.setFrmSalescash(this);
			byr.open();
			startTrx();
		}
	}

	private FormBayar byr;
	private Calendar trxDateCal;

	private void txtQtyFocusGained(FocusEvent evt) {
		txtQty.getOrgComponent().getControl().selectAll();
	}

	private void txtQtyFocusLost(FocusEvent evt) {
		if (txtQty.getValue().doubleValue() == 0) {
			txtQty.setValue(BigDecimal.valueOf(1));
		}
	}

	private void tableDataKeyPressed(KeyEvent evt) {
		if (evt.keyCode == SWT.CR) {
			doEditdata();
		} else if (evt.keyCode == SWT.DEL) {
			doDelete();
		}
	}

	private void doDelete() {
		int i = tableData.getSelectionIndex();

		MessageBox messageBox = new MessageBox(dialogShell, SWT.OK | SWT.CANCEL
				| SWT.ICON_WARNING);
		messageBox
				.setMessage("Anda Yakin akan menghapus data yang anda pilih?");
		if (messageBox.open() == SWT.OK) {
			if (i > -1) {
				GridItem itm = tableData.getItem(i);
				String code = itm.getText(0);
				try {
					AyukuDB.getPersist().executeUpdate(
							"delete from trx_temp_sale where barcode=?", code);
					doReloadList();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

		}

	}

	private void doEditdata() {
		/*
		 * Point[] ps = tableData.getCellSelection(); Point p = ps[0];
		 * 
		 * System.out.println("x: " + p.x +", y: "+ p.y);
		 */
		int i = tableData.getSelectionIndex();
		if (i > -1) {
			GridItem itm = tableData.getItem(i);
			String code = new String(itm.getText(0));
			EditItemKasir d = new EditItemKasir(dialogShell, SWT.NULL);
			d.setKode(code);
			d.open();
			try {
				doRecalculateRow(code);
				doReloadList();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			txtCode.setFocus();
		}
	}

	private void tableDataMouseDoubleClick(MouseEvent evt) {
		doEditdata();
	}

	private void text1KeyPressed(KeyEvent evt) {
		System.out.println("text1.keyPressed, event=" + evt);
		// TODO add your code for text1.keyPressed
	}

}
